package rpc

import (
	"common/config"
	userApi "common/proto/api"
	"fmt"
	_ "github.com/mbobakov/grpc-consul-resolver" // It's important
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

var (
	HelloClient userApi.GreeterClient
	UserClient  userApi.UserServiceClient //用户的客户端
)

/*
*
初始化 consul client
*/
func InitGateClient() {
	consulInfo := config.Conf.Consul
	helloConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", consulInfo.Host, consulInfo.Port, consulInfo.Register.Name),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		zap.S().Fatal("helloConn初始化失败")
	}
	HelloClient = userApi.NewGreeterClient(helloConn)

	userConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", consulInfo.Host, consulInfo.Port, consulInfo.Register.Name),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		zap.S().Fatal("userConn初始化失败")
	}
	UserClient = userApi.NewUserServiceClient(userConn)
}
